//
//  PeopleListViewController.m
//  AILove
//
//  Created by 甘世清 on 2023/12/15.
//

#import "PeopleListViewController.h"
#import "CCDraggableContainer.h"
#import "PeopleView.h"
#import "PeopleRefreshView.h"
#import "PeopleOneView.h"
@interface PeopleListViewController ()<
CCDraggableContainerDataSource,
CCDraggableContainerDelegate
>

@property (nonatomic, strong) CCDraggableContainer *container;
@property (nonatomic, strong) UIImageView * biaoshiImage;
 
@property (nonatomic , strong) PeopleRefreshView * refreshView;
@end

@implementation PeopleListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.zx_hideBaseNavBar = YES;
  
}
- (void)SQ_UIViewForSelfView{
    [self.view addSubview:self.container];
    [self.view addSubview:self.refreshView];
    WS(weakSelf);
    [self.refreshView setRefreshBlock:^{
        [weakSelf SQ_Header];
    }];
    
    NSMutableArray * array = [[NSMutableArray alloc]initWithObjects:@"全部",@"男性",@"女性", nil];
    for (int x = 0; x< array.count; x++) {
        NSString * string = [array objectAtIndex:x];
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:SQNSLocalString(string) forState:normal];
        btn.titleLabel.font = FONT_BOLD(18);
        [btn setTitleColor:SQHexColor(@"#FFFFFF", 0.7) forState:normal];
        btn.backgroundColor = UIColor.clearColor;
        btn.tag = x;
        [btn addTarget:self action:@selector(SQ_buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(80);
            make.height.mas_equalTo(44);
            make.top.equalTo(self.view).offset(TopHeight);
            make.centerX.equalTo(self.view).offset(80*x-80);
        }];
     
     }
    UserModel * model = [UserModel share];
    float xx = 0;
    if ([model.gender intValue]  == 2) {
        //全部
        xx = -80;
    } else  if ([model.gender intValue]== 1) {
      //nv
        xx = 80;
    }
    [self.view addSubview:self.biaoshiImage];
    [self.biaoshiImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(18);
        make.top.equalTo(self.view).offset(TopHeight+38);
        make.height.mas_equalTo(4);
      
        make.centerX.equalTo(self.view).offset(xx);
    }];
    [self SQ_Header];
    [self SQ_showOneView];
}
- (void)SQ_viewWillAppear:(BOOL)animated{
    if (self.jsonArray.count <=0) {
        self.refreshView.hidden = NO;
    } else {
        self.refreshView.hidden = YES;
    }
}
-(void)SQ_buttonClick:(UIButton*)btn{
    [YJProgressHUD SQ_showMsgWithoutView:@""];
    float xx = 0;
   NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    if (btn.tag  == 0) {
        [dic setObject:@"2" forKey:@"gender"];
        xx = -80;
    } else  if (btn.tag  == 1) {
        [dic setObject:@"0" forKey:@"gender"];
    }else{
        [dic setObject:@"1" forKey:@"gender"];
        xx = 80;
    }
    [HttpTool post:Url5 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        UserModel * model = [ UserModel share];
        model.gender = [dic objectForKey:@"gender"];
        [UserModel updateLocalUserModel:model];
        [self.biaoshiImage mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view).offset(xx);
        }];
        [self SQ_Header];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}

- (void)SQ_Header{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [HttpTool post:Url11 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
         NSArray * array = [PeopleModel mj_objectArrayWithKeyValuesArray:httpModel.data];
        [self SQ_PeopleList:array];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}
-(void)SQ_PeopleList:(NSArray*)array{
    [self.jsonArray removeAllObjects];
    if ([[UserModel share].gender isEqualToString:@"2"]) {
        [self.jsonArray addObjectsFromArray:array];
    } else {
        for (PeopleModel * model in array) {
            if (model.gender == [UserModel share].gender) {
                [self.jsonArray addObject:model];
            }
        }
    }
    [self.container reloadData];
    if (self.jsonArray.count >0) {
        self.refreshView.hidden = YES;
    } else {
        self.refreshView.hidden = NO;
    }
}
 

#pragma mark - CCDraggableContainer DataSource

- (CCDraggableCardView *)draggableContainer:(CCDraggableContainer *)draggableContainer viewForIndex:(NSInteger)index {
    
    PeopleView *cardView = [[PeopleView alloc] initWithFrame:draggableContainer.bounds];
    [cardView SQ_data:[self.jsonArray objectAtIndex:index]];
    WS(weakSelf);
    [cardView.leftButton setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf SQ_peopleModel:[weakSelf.jsonArray objectAtIndex:index] type:NO isbutton:YES];
       
    }];
    [cardView.loveButton setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf SQ_peopleModel:[weakSelf.jsonArray objectAtIndex:index] type:YES isbutton:YES];
       
    }];
    return cardView;
}

-(void)SQ_peopleModel:(PeopleModel*)model type:(BOOL)isLove isbutton:(BOOL)isbut{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:model.userId forKey:@"roleId"];
    if (isLove) {
        [dic setObject:@"1" forKey:@"type"];
    } else {
        [dic setObject:@"0" forKey:@"type"];
    }
    WS(weakSelf);
    [HttpTool post:Url14 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        if (isbut) {
            if (isLove) {
                [weakSelf.container removeFormDirection:CCDraggableDirectionRight];
            } else {
                [weakSelf.container removeFormDirection:CCDraggableDirectionLeft];
            }
        }
        if (isLove) {
            ProfileViewController * vc = [[ProfileViewController alloc]init];
            vc.model = model;
            [self SQ_pushViewControllerFull:vc];
        } 
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
  
}


- (NSInteger)numberOfIndexs {
    return self.jsonArray.count;
}

#pragma mark - CCDraggableContainer Delegate

- (void)draggableContainer:(CCDraggableContainer *)draggableContainer draggableDirection:(CCDraggableDirection)draggableDirection widthRatio:(CGFloat)widthRatio heightRatio:(CGFloat)heightRatio index:(NSInteger)index {
    WS(weakSelf);
    if (draggableDirection == CCDraggableDirectionLeft) {
        [weakSelf SQ_peopleModel:[weakSelf.jsonArray objectAtIndex:index] type:NO isbutton:NO];
    }
    if (draggableDirection == CCDraggableDirectionRight) {
        [weakSelf SQ_peopleModel:[weakSelf.jsonArray objectAtIndex:index] type:YES isbutton:NO];
    }
}

- (void)draggableContainer:(CCDraggableContainer *)draggableContainer cardView:(CCDraggableCardView *)cardView didSelectIndex:(NSInteger)didSelectIndex {
    ProfileViewController * vc = [[ProfileViewController alloc]init];
    vc.model = [self.jsonArray objectAtIndex:didSelectIndex];
    [self SQ_pushViewControllerFull:vc];
}

- (void)draggableContainer:(CCDraggableContainer *)draggableContainer finishedDraggableLastCard:(BOOL)finishedDraggableLastCard {
    DTLog(@"完了");
    [self.jsonArray removeAllObjects];
    self.refreshView.hidden = NO;
}

-(void)SQ_showOneView{
    if ([[Manager SQ_stringForKey:@"oneView"] isNotEmpty]) {
        return;
    }
    PeopleOneView * vc = [[PeopleOneView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, SCREEN_HEIGHT)];
    LSTPopView * pop = [LSTPopView initWithCustomView:vc
                                           parentView:nil
                                             popStyle:LSTPopStyleNO dismissStyle:LSTDismissStyleNO];
    pop.hemStyle = LSTHemStyleCenter;
    [pop pop];
    LSTPopViewWK(pop)
    WS(weakSelf);
    [vc setDismissBlock:^{
        [weakSelf SQ_pingjia];
        [wk_pop dismiss];
    }];
    
}

-(void)SQ_pingjia{
    if ([[Manager SQ_stringForKey:@"PF"] isNotEmpty]) {
        return;
    }
    [Manager SQ_SetObject:@"PF" FormKey:@"PF"];
    if ([SKStoreReviewController respondsToSelector:@selector(requestReviewInScene:)]) {
        NSArray *array =[[[UIApplication sharedApplication] connectedScenes] allObjects];
        UIWindowScene *windowScene = (UIWindowScene *)array[0];
        [SKStoreReviewController requestReviewInScene:windowScene];
    }
}

- (CCDraggableContainer *)container{
    if (!_container) {
        _container = [[CCDraggableContainer alloc]initWithFrame:self.view.frame style:CCDraggableStyleUpOverlay];
        _container.delegate = self;
        _container.dataSource = self;
    }
    return _container;
}

- (UIImageView *)biaoshiImage{
    if (!_biaoshiImage) {
        _biaoshiImage = [[UIImageView alloc]init];
        _biaoshiImage.image = SQGetImage(@"按钮背景");
        _biaoshiImage.clipsToBounds = YES;
        _biaoshiImage.contentMode = UIViewContentModeScaleAspectFill;
        _biaoshiImage.layer.cornerRadius = 2;
        _biaoshiImage.layer.masksToBounds = YES;
    }
    return _biaoshiImage;
}

- (PeopleRefreshView *)refreshView{
    if (!_refreshView) {
        _refreshView = [[PeopleRefreshView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, SCREEN_HEIGHT)];
        _refreshView.hidden = YES;
    }
    return _refreshView;
}
 
@end
